package org.ccg.servlet

import java.io.PrintWriter
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class LoginServlet : HttpServlet() {
    override fun doPost(request: HttpServletRequest, response: HttpServletResponse) {
        // 设置请求和响应编码
        request.characterEncoding = "UTF-8"
        response.contentType = "text/html;charset=UTF-8"
        // 获取参数
        println("1:   " + request.characterEncoding)
        val param = request.getParameter("1111")
        println("username1:   " + param)
        // 构造JSON数据
        var jsonData = ""
        val username = request.getParameter("username")
        val pwd = request.getParameter("pwd")
        println("username:   " + username)
        println("pwd:   " + pwd)
        if ("123" == username && "123" == pwd) {
            jsonData = "{\"key\":\"va1111lue\"}"
        } else {
            jsonData = "{\"key\":\"123\"}"
        }
        // 获取输出流
        val out: PrintWriter = response.writer
        // 向客户端写JSON数据
        out.print(jsonData)
        // 清空缓冲区，结束响应
        out.flush()
        out.close()
    }

//    override fun doGet(request: HttpServletRequest, response: HttpServletResponse) {
////        super.doGet(request, response)
////        // 设置响应内容类型
////        response.setContentType("application/json")
////        // 设置字符编码
////        response.setCharacterEncoding("UTF-8")
//        val username = request.getParameter("username")
//        val pwd = request.getParameter("pwd")
//        println("username:   " + username)
//        println("pwd:   " + pwd)
//        // 构造JSON数据
//        val jsonData = "{\"key\":\"value\"}"
//        // 获取输出流
//        val out: PrintWriter = response.getWriter()
//        // 向客户端写JSON数据
//        out.print(jsonData)
//        // 清空缓冲区，结束响应
//        out.flush()
//        out.close()
//    }


//    override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {
//        req.characterEncoding = "utf-8"
//        doGet(req, resp)
//    }
//    override fun doGet(request: HttpServletRequest, response: HttpServletResponse) {
//        val username = request.getParameter("username")
//        val password = request.getParameter("pwd")
//        println("username   $username   password   $password")
//        // 用户名或密码为空
//        response.setContentType("text/json;charset=utf-8")
//        response.getWriter().write("用户名或密码不能为空")
//    }
}